#include "key.h"
#include "picture.h"
#include "Beep.h"

// uint32_t value_sw_transient,value_sw_last_transient,value_sw;//键值
// uint8_t cnt_shake_sw;
// uint8_t	value_sw_change;
uint8_t vave1Position,vave2Position;
BYTE_t  LimitStatus;
Key_Struct_type    Key_Struct;
uint16_t key_option_time;

extern volatile uint32_t timer_2min;
extern volatile uint8_t Motor_flg_test1;
extern volatile bool MOTOR_START;

void limit_sw_init()
{
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOA);//限位输入检测
	gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9);
	gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_8 | GPIO_PIN_9);
}
//-----------------------------------------------------------------//
//函数名称： 
//函数功能： 
//输入参数： 无
//输出参数： 无
//返 回 值： 
//-----------------------------------------------------------------//
void Valve1Move(uint8_t posi)
{
	static uint8_t old_pos = 0, ISmove = 0;

	if (old_pos == posi && 0 == ISmove)
		return;
	else
		gpio_bit_set(MOTOR1_GPIO_PORT, MOTOR1_PIN);//电机1运转

	ISmove = 1;
	if (0 == gpio_input_bit_get(GPIOC, GPIO_PIN_6))
		vave1Position = limit1; //在限位开关1位置
	else if (0 == gpio_input_bit_get(GPIOC, GPIO_PIN_7))
		vave1Position = limit2; //在限位开关2位置
	else if (0 == gpio_input_bit_get(GPIOC, GPIO_PIN_8))
		vave1Position = limit3; //在限位开关3位置
	else if (0 == gpio_input_bit_get(GPIOC, GPIO_PIN_9))
		vave1Position = limit4; //在限位开关4位置
	else
		vave1Position = 0; //不在限位开关位置

	if (posi == vave1Position)
	{
		gpio_bit_reset(MOTOR1_GPIO_PORT, MOTOR1_PIN);//电机1停止
		old_pos = vave1Position;//记录当前位置值
		ISmove = 0;
	}
}

//-----------------------------------------------------------------//
//函数名称： 
//函数功能： 
//输入参数： 无
//输出参数： 无
//返 回 值： 
//-----------------------------------------------------------------//
void key_init()
{
	rcu_periph_clock_enable(RCU_GPIOE);//输入
	gpio_init(GPIOE, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14);
	gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}
//-----------------------------------------------------------------//
//函数名称： 
//函数功能： 
//输入参数： 无
//输出参数： 无
//返 回 值： 
//-----------------------------------------------------------------//
void SW_Scan()
{

	uint32_t key_value = 0;
	//	if(0==Flag.IsLocked)
	//	{
	if (gpio_input_bit_get(GPIOE, GPIO_PIN_14))
		key_value &= (~STATIC_ModeKey); // s4
	else
		key_value |= STATIC_ModeKey;

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_8))
		key_value &= (~HeightKey); // s8
	else
		key_value |= HeightKey;

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_9))
		key_value &= (~WeightKey); // s7
	else
		key_value |= WeightKey;

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_10))
		key_value &= (~UpKey); // s6
	else
		key_value |= UpKey;

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_11))
		key_value &= (~DownKey); // s5
	else
		key_value |= DownKey;

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_13))
		key_value &= (~NURSE_ModeKey); // s3
	else
		key_value |= NURSE_ModeKey;

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_12))
		key_value &= (~WaveKey); // s2
	else
		key_value |= WaveKey;
	//	}

	if (1 == Flag.IsLocked)
	{
		if (0 == key_value)
			Flag.PageNO = page_home; //回主界面
		else
			Flag.PageNO = page_lock; //锁屏提醒页面

		key_value = 0; //锁键按键无效
	}

	if (gpio_input_bit_get(GPIOE, GPIO_PIN_7))
		key_value &= (~LockKey); // s9 锁键
	else
		key_value |= LockKey;

	switch (Key_Struct.KeyStatus)
	{

	case WaitStatus: //等待状态  如果先前是初始态，即无动作

		if (key_value)
		{
			// if (WaveKey == key_value)
			// {
			// 	Key_Struct.KeyRelease = 1;
			// }
			
			Key_Struct.KeyStatus = PressStatus; //更新键的状态，普通被按下
			Key_Struct.KeyValue = key_value;
		}
		break;

	case PressStatus: //按下状态

		if (key_value == Key_Struct.KeyValue)
		{

			Key_Struct.KeyStatus = ReleaseStatus;  //转换到计时态
			Key_Struct.KeyPress = 1;   //按键按下的标志，适合在按键按下时操作
			Key_Struct.KeyTimeCounts = 0;
		}
		else
		{
			Key_Struct.KeyStatus = WaitStatus; //键没被按着，回初始态，说明是干扰
		}
		break;

	case ReleaseStatus: //等待释放状态
						//						if((key_value==0)||(key_value!=Key_Struct.KeyValue))
		if (0 == key_value)
		{
			Key_Struct.KeyTimeReleaseCounts++;
			if (Key_Struct.KeyTimeReleaseCounts > 1)
			{
				Key_Struct.KeyStatus = WaitStatus;
				Key_Struct.KeyRelease = 1;
				Key_Struct.KeyTimeReleaseCounts = 0;
			}

			//							KeyPress=1;//返回1,一次完整的普通按键
			//程序进入这个语句块，说明已经有2次以上10ms的中断，等于已经消抖
			//那么此时检测到按键被释放，说明是一次普通短按
		}
		else if (key_value != Key_Struct.KeyValue) //等待释放过程中又按了一个键（多键）
		{
			Key_Struct.KeyTimeReleaseCounts = 0;
			Key_Struct.KeyStatus = WaitStatus;
		}
		else //在计时态，检测到键还被按着
		{
			Key_Struct.KeyTimeReleaseCounts = 0;
			//							if((++Key_Struct.KeyTimeCounts>KeyLongTime)&&(key_value==Key_Struct.KeyValue))
			if (++Key_Struct.KeyTimeCounts > KeyLongTime)
			{
				Key_Struct.KeyStatus = LongStatus;
				Key_Struct.LongKeyFlag = 1;//长按标志位
				Key_Struct.KeyTimeCounts = 0;
			}
		}

		break;
	case LongStatus://长按状态
		if (key_value == 0)
		{
			Key_Struct.KeyStatus = WaitStatus;//等待状态
			Key_Struct.KeyValue = 0;
			//Key_Struct.LongKeyRelease=1;
			Key_Struct.KeyTimeCounts = 0;
		}
		break;
	}
}
//-----------------------------------------------------------------//
//函数名称： void KeyFunHdl()
//函数功能： 
//输入参数： 无
//输出参数： 无
//返 回 值： 
//-----------------------------------------------------------------//
void KeyFunHdl()
{
	if (0 == Flag.wifiKey_time) // wifi重置
	{
		Flag.wifiKey_No = 0;
	}

	if (0 == Flag.BeepKey_time) // 蜂鸣器关闭按钮重置
	{
		Flag.BeepKey_off = 0;
	}

	if (Flag.wifiKey_No >= 4) //按5下（第一下不计在内）
	{
		Flag.mqtt_conected = 0;
		Flag.wifiReset = 1;//wifi复位
	}

	if (Flag.BeepKey_off >= 1) //按2下（第一下不计在内）
	{
		if (Power_stop_Flg == 2)
		{
			warn_button = 4;
			// warn_state = 0;
			// Warning_time_s = 0;
			// warn_para.LowPress_time = 0;
			stop_alarm();
		}
		// stop_alarm();
		// Sound_off_icon();
		// Low_yellow_led_Off();
		// Flag.PageNO = page_home;
	}

	if (0 == Flag.LockCnt) //时间到，自动锁键
	{
		Flag.IsLocked = 1;
		LedOn();
		KeyBackLed_OFF();//硬件取消此功能
		//Flag.PageNO = page_home; //回主界面
		Flag.LockCnt = -1;
	}

	if ((1 == Flag.KeepUpKey) && (UpKey == Key_Struct.KeyValue)) //持续加
	{
		Flag.LockCnt = def_lock; //锁键时间 30000
		if (0 == Flag.KeyKeepCnt)
		{
			Flag.KeyKeepCnt = KeyKeepFre;//KeyKeepFre 100
			switch (Flag.PageNO)//界面标志位
			{
			case page_hight:
				if (SettingPARA.hight < hight_max)
					SettingPARA.hight++;
				else
					SettingPARA.hight = hight_max;
				break;
			case page_weight:
				if (SettingPARA.weight < weight_max)
					SettingPARA.weight++;
				else
					SettingPARA.weight = weight_max;
				break;
			case page_wave:

				if (SettingPARA.WaveCycle_lenth < WaveLenth_max)
					SettingPARA.WaveCycle_lenth++;
				else
					SettingPARA.WaveCycle_lenth = WaveLenth_max;
				break;
			}
		}
	}
	if ((1 == Flag.KeepDownKey) && (DownKey == Key_Struct.KeyValue)) //持续减
	{
		Flag.LockCnt = def_lock; //锁键时间 30000
		if (0 == Flag.KeyKeepCnt)
		{
			Flag.KeyKeepCnt = KeyKeepFre;
			switch (Flag.PageNO)
			{
			case page_hight:
				if (SettingPARA.hight > hight_min)
					SettingPARA.hight--;
				else
					SettingPARA.hight = hight_min;
				break;
			case page_weight:
				if (SettingPARA.weight > weight_min)
					SettingPARA.weight--;
				else
					SettingPARA.weight = weight_min;
				break;
			case page_wave:
				if (SettingPARA.WaveCycle_lenth > WaveLenth_min)
					SettingPARA.WaveCycle_lenth--;
				else
					SettingPARA.WaveCycle_lenth = WaveLenth_min;
			break;
			}
		}
	}
	if (0 == Key_Struct.KeyValue)
	{
		Flag.KeepUpKey = 0;
		Flag.KeepDownKey = 0;
		Flag.KeyKeepCnt = 0;
	}
}
//-----------------------------------------------------------------//
//函数名称： 
//函数功能： 
//输入参数： 无
//输出参数： 无
//返 回 值： 
//-----------------------------------------------------------------//
void SW_Process()
{

	SW_Scan(); //按键扫描

	if (1 == Key_Struct.KeyRelease) //短按放开
	{
		Key_Struct.KeyRelease = 0; //此处清0
	}

	if (1 == Key_Struct.KeyPress) //有键按下
	{
		Key_Struct.KeyPress = 0;
		switch (Key_Struct.KeyValue)
		{
			//身高----------------------------------
		case HeightKey:

			Flag.PageNO =page_hight;// 
			break;

			//体重----------------------------------
		case WeightKey:

			Flag.PageNO = page_weight;
			break;

			//静态模式----------------------------------
		case STATIC_ModeKey:
		
			if (page_home == Flag.PageNO)
			{
				SettingPARA.mode = MODE_STATIC;//静态模式
				TFTLCD_Fill(bodongleftime_x, bodongleftime_y, bodongleftime_x + 180, bodongleftime_y + 24, MY_BLUE);
			}
			// SettingPARA.mode = MODE_STATIC;//静态模式

			break;

			//护理模式----------------------------------
		case NURSE_ModeKey:

			if (page_home == Flag.PageNO)
			{
				SettingPARA.mode = MODE_NURSE;//护理模式
				TFTLCD_Fill(bodongleftime_x, bodongleftime_y, bodongleftime_x + 180, bodongleftime_y + 24, MY_BLUE);
			}
			// SettingPARA.mode = MODE_NURSE;//护理模式

			break;

			//波动---------------------------------
		case WaveKey:

			Flag.PageNO = page_wave;
			SettingPARA.mode = MODE_WAVE;

			break;

			//上键----------------------------------
		case UpKey:

			switch (Flag.PageNO)
			{
			case page_hight:
				if (SettingPARA.hight < hight_max)
					SettingPARA.hight++;
				else
					SettingPARA.hight = hight_max;
				break;
			case page_weight:
				if (SettingPARA.weight < weight_max)
					SettingPARA.weight++;
				else
					SettingPARA.weight = weight_max;
				break;

			case page_wave:

				SettingPARA.mode = MODE_WAVE;
				if (SettingPARA.WaveCycle_lenth < WaveLenth_max)
					SettingPARA.WaveCycle_lenth++;
				else
					SettingPARA.WaveCycle_lenth = WaveLenth_max;
				break;
			}

			break;
			//下键----------------------------------
		case DownKey:
			switch (Flag.PageNO)
			{
			case page_hight:
				if (SettingPARA.hight > hight_min)
					SettingPARA.hight--;
				else
					SettingPARA.hight = hight_min;
				break;

			case page_weight:
				if (SettingPARA.weight > weight_min)
					SettingPARA.weight--;
				else
					SettingPARA.weight = weight_min;
				break;

			case page_wave:

				SettingPARA.mode = MODE_WAVE;
				if (SettingPARA.WaveCycle_lenth > WaveLenth_min)
					SettingPARA.WaveCycle_lenth--;
				else
					SettingPARA.WaveCycle_lenth = WaveLenth_min;

				break;
			}

			break;
			//锁键----------------------------------
		case LockKey:

			if (Flag.wifiKey_time)
			{
				Flag.wifiKey_No++;
			}
			

			if (Flag.BeepKey_time)
			{
				Flag.BeepKey_off++;
			}
			Flag.BeepKey_time = BeepKeytTime;//#define WifiKeytTime 1500 //按键间隔最大时间2s

			
			Flag.wifiKey_time = WifiKeytTime;//#define WifiKeytTime 1500 //按键间隔最大时间2s
			Flag.PageNO = page_home; //回主界面
			break;
		}

		if (Key_Struct.KeyValue != LockKey)
			Flag.LockCnt = def_lock; //锁键时间 30000
		
	}

	if (1 == Key_Struct.LongKeyFlag) //长按
	{
		Key_Struct.LongKeyFlag = 0;
		switch (Key_Struct.KeyValue)
		{
		//锁键----------------------------------
		case LockKey:
			Flag.IsLocked ^= 1;

			if (Flag.IsLocked)
			{
				LedOff();
				KeyBackLed_OFF();
				Flag.LockCnt = 0;		 //锁键时间清零
				Flag.PageNO = page_home; //回主界面
			}
			else
			{
				LedOff();
				KeyBackLed_ON();
				Flag.LockCnt = def_lock; //30000
			}
			break;
		//身高---------------------------
		case HeightKey:
			if (Flag.PageNO == page_hight)
				SettingPARA.hight = def_hight;

			break;
		//体重---------------------------
		case WeightKey:
			if (Flag.PageNO == page_weight)
				SettingPARA.weight = def_weight;

			break;
		//波动---------------------------------
		case WaveKey:
			if (Flag.PageNO == page_wave)
				SettingPARA.WaveCycle_lenth = def_wavelenth;

			break;
		//上键---------------------------
		case UpKey:
			if ((Flag.PageNO == page_weight) || (Flag.PageNO == page_hight) || (Flag.PageNO == page_wave) || (Flag.PageNO == page_autoturn ))
			{
				Flag.KeepUpKey = 1;
			}
			break;
		//下键---------------------------
		case DownKey:
			if ((Flag.PageNO == page_weight) || (Flag.PageNO == page_hight) || (Flag.PageNO == page_wave) || (Flag.PageNO == page_autoturn ))
			{
				Flag.KeepDownKey = 1;
			}
			break;
		}
		if (Key_Struct.KeyValue != LockKey)
			Flag.LockCnt = def_lock; //锁键时间 30000
	}

	KeyFunHdl();
}
